var ns, toast, actionsheet, UIAlbumBrowser,fileBrowser,superFile;
var app = new Vue({
    el: '#app',
    data: {
        form:{},
        fileList:[],
        up_files:[],
        filePath:'',
        fileType:'',

    },
    methods: {
        // beforeread(e){
        //     console.log(JSON.stringify(e));
        //     return true;
        // },
        // afterRead(file){
        //     console.log(JSON.stringify(file));
            // const formData=new FormData();

            // formData.append('file',fileObj.file);
            // console.log(JSON.stringify(formData));
            // var option={};
            // option.action='uploadfileone';
            // option.files=formData;
            // ns.post(option, function(succeed, data, err) {
            //     console.log(JSON.stringify(data));
            //     console.log(JSON.stringify(err));
            // });
            // this.$api.Up_files(formData).then((res)=>{
            //     console.log(JSON.stringify(res));
            //     this.turn.up_files.push(res.id);
            //     fileObj.status="done";
            //     this.fileLoading=false;
            //     console.log(JSON.stringify(this.turn.up_files));
            // }).catch((e)=>{
            //     console.log(JSON.stringify(e,11));
            // })
            // console.log(JSON.stringify(file));
        // },
        handleFile:function(){
            var that=this;
            fileBrowser.open({
                confirm:true
            },function(ret) {
                fileBrowser.close();
                if (ret && ret.status) {
                    const url = ret.url || ""
                    // var i = url.lastIndexOf('/');
                    // var j = url.length;
                    // that.form.CredentialName = url.slice(i+1,j);
                    // 多规格图片上传
                    const params = {
                        action: "uploadfileone",
                    }
                    ns.post(params, function (succeed, data, err) {
                        if (succeed) {
                            that.form.CredentialName=data.file.FileName;
                            that.form.Credential=data.file.Path;
                            that.fileType=data.file.ContentType;
                            that.filePath=data.file.FullPath;
                            alert(JSON.stringify(data))
                            // alert(JSON.stringify(that.filePath))
                            
                            // console.log(JSON.stringify(err))
                        }
                    }, {
                        files: {
                            file: [url]
                        }
                    })
                    return;
                }
                console.log(JSON.stringify(ret));
                console.log(JSON.stringify(ret))
                return false;
            });
            // fileBrowser.open(function(ret,err){
            //     console.log('11111111111');
                // if (ret) {
                //     console.log(JSON.stringify(ret));
                //     fileBrowser.close();
                //     //alert(ret.url);
                    
                //     url11 = ret.url;
                //     var i = url11.lastIndexOf('/');
                //     var j = url11.length;
                //     var name11 = url11.slice(i+1,j);
                //     //alert(url11.slice(i+1,j));
                   
                //     var model = api.require('model');
                //     model.uploadFile({
                //             report : false,
                //             data : {
                //                     file : {
                //                             name : name11,
                //                             url : url11
                //                     }
                //             }
                //     }, function(ret, err) {
                //             if (ret) {
                //                 console.log(JSON.stringify(ret));
                //             } else {
                //                 console.log(JSON.stringify(err));
                //             }
                //     });
                //  } 
            // else {
            //     console.log(JSON.stringify(err));
            // }
            // });
        },
        open_flie:function(){
            var that=this;
            console.log(JSON.stringify(that.filePath));
            console.log(JSON.stringify(that.form.CredentialName));
            api.download({
                // 
                url:that.filePath,
                savePath:'fs://app/'+that.form.CredentialName,
                report:true,
                cache:true,
                allowResume:true
            }, function(ret, err){
                if(ret.state==1){
                    if(api.systemType=='ios'){
                        superFile.open({
                            path:ret.savePath,
                        })

                    }
                    if(api.systemType=="android"){
                        console.log(JSON.stringify(api.fsDir))
                        console.log(2);
                        console.log(JSON.stringify(ret.savePath));
                        superFile.open({
                            path:ret.savePath,
                        })


                        // superFile.init(function(ret){
                        //     console.log(JSON.stringify(ret));
                            
                        //     if(ret.eventType=="onViewInitFinished"){
                                
                        //     }
                        // })
                    }
                }
            })
        },
        

        do_saveshopkey:function(){
            var that=this;
            if (!that.form.AccountNumber) {
                api.toast({
                    msg: '商户号不能为空',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            if (!that.form.Key) {
                api.toast({
                    msg: '证书key不能为空',
                    duration: 3000,
                    location: 'bottom'
                });
                return;
            }
            var options={};
            options.action='editbusinessaccountinfo';
            options.ID=that.form.ID;
            options.AccountNumber=that.form.AccountNumber;
            options.Key=that.form.Key;
            options.File=that.form.Credential;
            options.FileName=that.form.CredentialName;
            console.log(JSON.stringify(options));
            ns.post(options, function(succeed, data, err) {
                if(succeed){
                        api.toast({
                            msg: '修改成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name: 'edit_shop'
                        });
                        setTimeout(function() {
                            api.closeWin();
                        }, 500);
                       
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
            
        }

         
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    fileBrowser = api.require('fileBrowser');
    superFile = api.require('superFile');
    console.log("UIAlbumBrowser--------"+JSON.stringify(UIAlbumBrowser))
    console.log("fileBrowser--------"+JSON.stringify(fileBrowser))
    bmLocation = api.require('bmLocation');
    app.form = api.pageParam.item;
    console.log(JSON.stringify(app.form));

    // app.get_data();

};

     